import { Toast } from 'vant'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/home'
}, {
    path: '/home',
    name: 'home',
    component: Home,
    meta: {
        keepAlive: true
    }
}, {
    path: '/hotellist',
    name: 'hotellist',
    component: () =>
        import('../views/HotelList.vue'),
    meta: {
        keepAlive: true
    }
}, {
    path: '/hotel',
    name: 'hotel',
    component: () =>
        import('../views/Hotel.vue'),
    // meta: {
    //     keepAlive: true
    // }
}, {
    path: '/other/',
    name: 'other',
    redirect: '/other/food',
    component: () =>
        import('../views/Other.vue'),
    children: [{
        path: 'food',
        name: 'food',
        component: () =>
            import('../views/other/Food.vue'),
    }, {
        path: 'play',
        name: 'play',
        component: () =>
            import('../views/other/Play.vue'),
    }, {
        path: 'news',
        name: 'news',
        component: () =>
            import('../views/other/News.vue'),
    },
    ]
}, {
    path: '/detail/:id',
    name: 'detail',
    component: () =>
        import('../views/other/Detail.vue'),
}, {
    path: '/login',
    name: 'login',
    component: () =>
        import('../views/Login.vue'),
}, {
    path: '/register',
    name: 'register',
    component: () =>
        import('../views/Register.vue'),
}, {
    path: '/personalcenter',
    name: 'personalcenter',
    component: () =>
        import('../views/myhome/PersonalCenter.vue'),
    meta: {
        keepAlive: true
    }
}, {
    path: '/setting',
    name: 'setting',
    component: () =>
        import('../views/myhome/Setting.vue'),
    meta: {
        keepAlive: true
    }
}, {
    path: '/orderlist',
    name: 'orderlist',
    component: () =>
        import('../views/OrderList.vue'),
}, {
    path: '/order',
    name: 'order',
    component: () =>
        import('../views/Order.vue'),
}, {
    path: '/vip',
    name: 'vip',
    component: () =>
        import('../views/myhome/Vip.vue'),
    meta: {
        keepAlive: true
    }
}, {
    path: '/commoninfo',
    name: 'commoninfo',
    component: () =>
        import('../views/myhome/CommonInfo.vue'),
    meta: {
        keepAlive: true
    }
}, {
    path: '/bindbankcard',
    name: 'bankcard',
    component: () =>
        import('../views/myhome/BankCard.vue'),
}, {
    path: '/addbankcard',
    name: 'addbankcard',
    component: () =>
        import('../views/myhome/AddBankCard.vue'),
}, {
    path: '/search',
    name: 'search',
    component: () =>
        import('../views/Search.vue'),
}, {
    path: '/orderdetail',
    name: 'orderdetail',
    component: () =>
        import('../views/OrderDetail.vue'),
},
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

export default router

// 路由守卫
router.beforeEach((to, from, next) => {
    const is_login = window.sessionStorage.getItem('uid')
    if (to.path === '/commoninfo' && !is_login) {
        Toast.fail('请先登录')
        setTimeout(() => {
            next('/login')
        }, 1000)
    } else if (to.path == '/order' && from.path == '/hotel' && !is_login) {
        console.log(from.path)
        Toast.fail('请先登录')
        setTimeout(() => {
            next('/login')
        }, 1000)
    } else if (to.path == '/bindbankcard' && !is_login) {
        Toast.fail('请先登录')
        setTimeout(() => {
            next('/login')
        }, 1000)
    } else {
        next()
    }
})

// 获取路由版本错误
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}